package com.code.splider.service.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.code.splider.dao.CornerMapper;
import com.code.splider.model.Corner;
import com.code.splider.service.CornerService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Service
public class CornerServiceImpl implements CornerService{
	
	@Resource
	private CornerMapper cornerMapper;

	@Override
	public void saveCorner(String jsonText) {
		try {
			if (null!=jsonText) {
				//删除全部
				cornerMapper.deleteAll();
				
				JSONObject jsonObject = JSONObject.fromObject(jsonText);
		        
		        JSONArray jsonArray = jsonObject.getJSONArray("rs");
		        for (int i = 0; i < jsonArray.size(); i++) {
		        	System.out.println("=====================================");
		        	String teams=jsonArray.getString(i);
		            
		            JSONObject jsonObject2 = JSONObject.fromObject(teams);
		            
		            String status = jsonObject2.getString("status");
		            System.out.println("..........状态:"+ status);
		            
		            if (!status.equals("-1")&&!status.equals("全")) {
		            	Corner corner=new Corner();
		            	corner.setStatus(status);
		            	
		            	String league = jsonObject2.getString("league");
		                JSONObject jsonObject_league = JSONObject.fromObject(league);
		                String n = jsonObject_league.getString("n");
		                corner.setCompetition(n);
		                System.out.println("..........赛事:"+ n);
		                
		                String host = jsonObject2.getString("host");
		                JSONObject jsonObject_host = JSONObject.fromObject(host);
		                String n_host = jsonObject_host.getString("n");
		                corner.setHost(n_host);
		                System.out.println("..........主队:"+ n_host);
		                
		                String guest = jsonObject2.getString("guest");
		                JSONObject jsonObject_guest = JSONObject.fromObject(guest);
		                String n_guest = jsonObject_guest.getString("n");
		                corner.setGuest(n_guest);
		                System.out.println("..........客队:"+ n_guest);
		                
		                String sd = jsonObject2.getString("sd");
		                JSONObject jsonObject_sd = JSONObject.fromObject(sd);
		                String f = jsonObject_sd.getString("f");
		                
		                JSONObject jsonObject_f = JSONObject.fromObject(f);
		                
		                if (jsonObject_f.containsKey("hrf")) {
		                	String hrf_sd = jsonObject_f.getString("hrf");
			            	corner.setHrfSd(hrf_sd);
			            	System.out.println("..........初让球:"+ hrf_sd);
						}
		                
		                if (jsonObject_f.containsKey("hcb")) {
		                	String hrf_hcb = jsonObject_f.getString("hcb");
			            	corner.setHrfHcb(hrf_hcb);
			            	System.out.println("..........初角球:"+ hrf_hcb);
						}
		                
		                if (jsonObject_f.containsKey("hdx")) {
		                	String hdx_sd = jsonObject_f.getString("hdx");
		            		corner.setHdxSd(hdx_sd);
		                    System.out.println("..........初大小球:"+ hdx_sd);
						}
		            	
		                if (jsonObject2.containsKey("f_ld")) {
		                	String f_ld = jsonObject2.getString("f_ld");
			                JSONObject jsonObject_f_ld = JSONObject.fromObject(f_ld);
			                
			                if (jsonObject_f_ld.containsKey("hrf")) {
			                	String hrf = jsonObject_f_ld.getString("hrf");
				                corner.setHrf(hrf);
				                System.out.println("..........即时让球:"+ hrf);
							}
			                
			                if (jsonObject_f_ld.containsKey("hdx")) {
			                	String hdx = jsonObject_f_ld.getString("hdx");
				                corner.setHdx(hdx);
				                System.out.println("..........即时大小球:"+ hdx);
							}
			                
			                if (jsonObject_f_ld.containsKey("hcbsp")) {
			                	String hcbsp = jsonObject_f_ld.getString("hcbsp");
				                corner.setHcbsp(hcbsp);
				                System.out.println("..........即时角球大赔率:"+ hcbsp);
							}
			                
			                if (jsonObject_f_ld.containsKey("gcbsp")) {
			                	String gcbsp = jsonObject_f_ld.getString("gcbsp");
				                corner.setGcbsp(gcbsp);
				                System.out.println("..........即时角球小赔率:"+ gcbsp);
							}
			                
			                if (jsonObject_f_ld.containsKey("hcb")) {
			                	String hcb = jsonObject_f_ld.getString("hcb");
				                corner.setHcb(hcb);
				                System.out.println("..........即时角球:"+ hcb);
							}
			                if (status.equals("未")) {
			                	 String rtime = jsonObject2.getString("rtime");
			                	 corner.setRtime(rtime);
				                 System.out.println("..........开始时间:"+ rtime);
			 				}else{
			 					
			 					if (jsonObject2.containsKey("plus")) {
			 						String plus = jsonObject2.getString("plus");
				                    JSONObject jsonObject_plus = JSONObject.fromObject(plus);
				                    
				                    if (jsonObject_plus.containsKey("ga")) {
				                    	String ga_plus = jsonObject_plus.getString("ga");
					                    corner.setGaPlus(ga_plus);
					                    System.out.println("..........客队进攻:"+ ga_plus);
									}
				                    if (jsonObject_plus.containsKey("hd")) {
				                    	String hd_plus = jsonObject_plus.getString("hd");
					                    corner.setHdPlus(hd_plus);
					                    System.out.println("..........主队危险进攻:"+ hd_plus);
									}
				                    
				                    if (jsonObject_plus.containsKey("gd")) {
				                    	String gd_plus = jsonObject_plus.getString("gd");
					                    corner.setGdPlus(gd_plus);
					                    System.out.println("..........客队危险进攻:"+ gd_plus);
									}
				                    
				                    if (jsonObject_plus.containsKey("hso")) {
				                    	String hso_plus = jsonObject_plus.getString("hso");
					                    corner.setHsoPlus(hso_plus);
					                    System.out.println("..........主队射正球门:"+ hso_plus);
									}
				                    
				                    if (jsonObject_plus.containsKey("gso")) {
				                    	String gso_plus = jsonObject_plus.getString("gso");
					                    corner.setGsoPlus(gso_plus);
					                    System.out.println("..........客队射正球门:"+ gso_plus);
									}
				                    
				                    if (jsonObject_plus.containsKey("hsf")) {
				                    	String hsf_plus = jsonObject_plus.getString("hsf");
					                    corner.setHsfPlus(hsf_plus);
					                    System.out.println("..........主队射偏球门:"+ hsf_plus);
									}
				                    
				                    if (jsonObject_plus.containsKey("gsf")) {
				                    	String gsf_plus = jsonObject_plus.getString("gsf");
					                    corner.setGsfPlus(gsf_plus);
					                    System.out.println("..........客队射偏球门:"+ gsf_plus);
									}
				                    
				                    if (jsonObject_plus.containsKey("hqq")) {
				                    	 String hqq_plus = jsonObject_plus.getString("hqq");
						                 corner.setHqqPlus(hqq_plus);
						                 System.out.println("..........主队球权:"+ hqq_plus);
									}
				                    
				                    if (jsonObject_plus.containsKey("gqq")) {
				                    	 String gqq_plus = jsonObject_plus.getString("gqq");
						                    corner.setGqqPlus(gqq_plus);
						                    System.out.println("..........客队球权:"+ gqq_plus);
									}
				                   
				                    if (jsonObject2.containsKey("rd")) {
				                    	String rd = jsonObject2.getString("rd");
					                    JSONObject jsonObject_rd = JSONObject.fromObject(rd);
					                    
					                    if (jsonObject_rd.containsKey("hg")) {
					                    	String hg = jsonObject_rd.getString("hg");
						                    corner.setHg(hg);
						                    System.out.println("..........主队得分:"+ hg);
										}
					                    
					                    if (jsonObject_rd.containsKey("gg")) {
					                    	String gg = jsonObject_rd.getString("gg");
						                    corner.setGg(gg);
						                    System.out.println("..........客队得分:"+ gg);
										}
					                    if (jsonObject_rd.containsKey("hc")) {
					                    	String hc = jsonObject_rd.getString("hc");
						                    corner.setHc(hc);
						                    System.out.println("..........主队角球:"+ hc);
										}
					                    
					                    if (jsonObject_rd.containsKey("gc")) {
					                    	String gc = jsonObject_rd.getString("gc");
						                    corner.setGc(gc);
						                    System.out.println("..........客队角球:"+ gc);
										} 
									}  
								}  
			 				}
						}
		                corner.setUpdateTime(new Date());
		                cornerMapper.insert(corner);
					}   
		        }
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}
